/*
 * @author: wizardc
 */

using DouEditor.Const;
using DouEditor.Utils;
using UnityEditor;
using UnityEngine;

namespace DouEditor.Asset
{
    /// <summary>
    /// 精灵图文件处理
    /// 和项目强相关的处理类，指定文件夹下的贴图会被设置为一致的参数
    /// </summary>
    public class SpriteMonitor : AssetPostprocessor
    {
        private const string UISprite = BuildConst.AtlasSpriteFolder;
        
        void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
        {
            for (int i = 0; i < movedAssets.Length; i++)
            {
                var path = movedAssets[i];
                ApplySpriteSetting(path);
            }
        }

        void OnPreprocessTexture()
        {
            ApplySpriteSetting(assetPath);
        }

        void OnPostprocessTexture()
        {
        }

        private void ApplySpriteSetting(string assetPath)
        {
            assetPath = EditorUtil.FormatPath(assetPath);
            var textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
            if (textureImporter == null)
            {
                return;
            }
            if (assetPath.StartsWith(UISprite))
            {
                if (textureImporter.textureType != TextureImporterType.Sprite)
                {
                    textureImporter.textureType = TextureImporterType.Sprite;
                }
                TextureMonitor.SetTexturePlatformSettings(textureImporter, TextureImporterFormat.ASTC_4x4, TextureWrapMode.Repeat, false, true);
            }
        }
    }
}
